<template>
	<!-- <view class="content">
		<image class="bg" src="/static/login/log_bg.png" mode="scaleToFill"></image> -->

	<view class="content" :style="{'background':kbackgroundColor}" style="min-height: 100vh">
		<hx-navbar title="寄售" :back="true" :fixed="false" :color="knavTitleColor" barPlaceholder="hidden"
			transparent="auto"></hx-navbar>
		<view class="container">
			<view class="sale_input">
				<view class="sale_title">设置价格</view>
				<view class="cell-item-ft">
					<text style="font-size: 52rpx;" class="color-14">￥</text>
					<input type="digit" v-model="input_price" :style="{'color': (ifsave ? '#000000': '#ff0000')}"
						@input="on_input" class="inputer" placeholder="输入寄售价格" placeholder-class="sale_holder">
				</view>
			</view>
			<view class="collection_info">
				<image :src="item.image" mode="aspectFit" class="collect_image"></image>
				<view class="collect_info">
					<view class="collect_title">链上信息</view>
					<view class="collect_item">
						<view class="keys">所属专辑</view>
						<view class="value">{{category.name}}</view>
					</view>
					<view class="collect_item">
						<view class="keys">合约地址</view>
						<view class="value webkit flex1">{{item.contract}}</view>
					</view>
				</view>
			</view>
			<view class="sale_info">
				<!-- <view class="sale_items">
					买入价:
					<view class="sale_value">
						￥{{info.input_price}}
					</view>
				</view> -->
				<view class="sale_items">
					<view class="cell-item-ft">
						综合服务费({{tradelist.fee || 0}}%)
						<!-- <image @click="btn_cost" class="cost-img" src="/static/detail/cost.png" mode="aspectFit">
						</image> -->
					</view>
					<view class="sale_value">
						-￥{{shouxufei.toFixed(2)||0}}
					</view>
				</view>
				<view class="sale_items">
					寄售收入:
					<view class="sale_value color-14">
						￥{{shouru.toFixed(2)||0}}
					</view>
				</view>
			</view>
			<view class="sale_tips">
				<view class="sale_title">
					寄售须知
				</view>
				<view class="sale_content">
					<view class="">
						1. 商品未出售前，用户可主动取消寄售；商品被下单后无法取消寄售。
					</view>
					<view class="">
						2. 您将获得除综合服务费（{{tradelist.fee || 0}}%）之外的所有收入
					</view>
					<view class="">
						3.商品出售成功后，售卖收入将默认进入用户钱包余额，钱包所有功能由第三方支付提供服务。
					</view>
					<view class="">
						4. 商品出售的增值收入部分，须请用户自行申报个人所得税。
					</view>
					<!-- <text>{{sale_desc}}</text> -->
				</view>
			</view>
			<view class="" style="height: 150rpx;"></view>
			<view class="bottom_boxx" :style="{background:ktabbarBgColor}">
				<view :style="{color:knormalColor}"><text style="font-size: 36rpx;">￥</text>{{input_price || 0}}</view>
				<view class="buy_btn" v-if="ifsave" :style="{background:kbuttonBgColor}" @tap="sale_before">立即寄售</view>
				<view class="buy_btn" v-if="!ifsave" :style="{background:kbuttonBgColor}">不可寄售</view>
			</view>
		</view>
		<uni-popup ref="codebox" type="center">
			<view class="cost-box">
				<view class="icon-img" @click="close">
					<n-icon name="close" size="36" color="#333"></n-icon>
				</view>
				<view class="cost-title">
					综合服务费详细说明
				</view>
				<view class="m-t-40 fsz24 color-6">
					1. 平台交易手续费{{(bili*0.48).toFixed(2)}}%
				</view>
				<view class="m-t-10 fsz24 color-6">
					2.支付通道手续费{{(bili*0.12).toFixed(2)}}%
				</view>
				<view class="m-t-10 fsz24 color-6">
					3.上链手续费:{{(bili*0.2).toFixed(2)}}%
				</view>
				<view class="m-t-10 fsz24 color-6">
					4.创作者分成:{{(bili*0.2).toFixed(2)}}%
				</view>
			</view>
		</uni-popup>
		<paypass ref="passbox" @confirm="set_done"></paypass>
	</view>
</template>


<script>
	import paypass from '@/components/pay_pass/pay_pass.vue'
	export default {
		components: {
			paypass
		},
		data() {
			return {
				info: {},
				id: '',
				input_price: '',
				shouxufei: 0,
				shouru: 0,
				type: 1,
				item: {},
				ifsave: true,
				category:{},
				tradelist:{}
			}
		},
		computed: {
			bili: function() {
				return this.$store.state.config.resale_fee
			},
			sale_desc() {
				return this.$store.state.config.sale_desc;
			},


		},

		onLoad(options) {
			this.id = options.id;
			if (!this.id) {
				this.$common.errorToShow('商品失效')
				uni.navigateBack()
				return;
			}
			this.getTradelist()
			let userToken = this.$db.get("userToken");
			let data = {
				sn: this.id
			}
			if (userToken) {
				data.token = userToken;
			}
			this.$api.getCollectionDetails(this.id, res => {
				if (res.status) {
					this.info = res.data;

					this.$api.getGoodsDetails(res.data.iid, res => {
						this.item = res.data
						this.author = this.item.author
						this.category = this.item.category
						console.log("this.item ---", this.item)
					})
				}
			})
			// this.$api.shopConfig(res => {
			// 	this.$store.commit('config', res)
			// })
		},
		methods: {
			getTradelist() {
				this.$api.getOpeningTrade(res => {
					if (res.status) {
						this.tradelist = res.data
					}
				})
			},
			close() {
				this.$refs.codebox.close()
			},
			btn_cost() {
				this.$refs.codebox.open()
			},
			on_input: function(e) {
				if (e.detail.value) {
					let value = parseFloat(e.detail.value);
					this.shouxufei = value * (this.tradelist.fee/100);
					this.shouru = value - this.shouxufei;
					// this.shouxufei = 0;
					// this.shouru = value;
					this.ifsave = true
				} else {
					this.shouxufei = 0;
					this.shouru = 0;
					this.ifsave = true
				}
			},
			checkPassword(){
				// this.$api.checkSecondPassword(){
					
				// }
			},
			sale_before: function() {
				this.type = 1
				//最高价
				var hPrice = this.item.highPrice / 100
				//最低
				var lPrice = this.item.lowPrice / 100
				console.log("this.item hPrice", hPrice)
				console.log("this.item --lPrice-", lPrice)
				if (this.input_price > hPrice || this.input_price <lPrice) {
					this.ifsave = false
                    this.$common.errorToShow('最大不能超过'+hPrice+'，最小不能低于'+lPrice)
				} else {
					uni.showModal({
						content: '确认寄售吗？',
						success: (res) => {
							if (res.confirm) {
								this.$api.checkSecondPassword({}, res => {
									if(res.status){
										let data = res.data
										if (data.secondPassword) {
											this.$refs.passbox.open();
										} else {
											this.$common.errorToShow('请设置交易密码', () => {
												uni.navigateTo({
													url: '/pages/user/settings/pay_pass/pay_pass'
												})
											});
										}
									}else{
										this.$common.errorToShow('网络异常');
									}
								})
							}
						}
					});
				}
			},

			sale: function(pass) {
				let data = {
					nftId: this.id,
					price: this.input_price * 100,
					secondPassword: pass,
					payWay: 2,
				}
				this.$api.sale(data, res => {
					if (res.status) {
						this.$common.successToShow("寄售成功", () => {
								uni.navigateBack({  
								    delta: 2 // 返回上两级页面  
								});//  uni.navigateBack()
						});
					} else {
						this.$common.errorToShow(res.message, () => {})
					}
				})

			},
			set_done: function(pass) {
				if (this.type == 1) {
					this.sale(pass)
				}
				if (this.type == 2) {
					this.give(pass)
				}
			},

		}
	}
</script>
<style lang="scss">
	.cost-box {
		width: 592rpx;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 45rpx;
		position: relative;

		.cost-title {
			font-size: 32rpx;
			text-align: center;
			font-weight: bold;
			color: #333333;
		}

		.icon-img {
			position: absolute;
			top: 45rpx;
			right: 45rpx;
		}
	}

	page {
		background-color: #F3F2F7;
	}

	.container {
		padding: 20rpx;
	}

	.bottom_boxx {
		width: 100%;
		height: 160rpx;
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 40rpx;
		font-size: 48rpx;
		color: #000;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 11;

		.buy_btn {
			width: 470rpx;
			height: 80rpx;
			border-radius: 100rpx;
			background: $btn-bg;
			text-align: center;
			line-height: 80rpx;
			font-size: 32rpx;
			color: black;
			font-weight: 400;
		}
	}

	.sale_info {
		width: 100%;
		background-color: $bg-box;
		padding: 0 30rpx 35rpx;
		border-radius: 0 0 20rpx 20rpx;
		margin-bottom: 20rpx;

		.sale_items {
			padding-top: 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 28rpx;
			color: #141519;

			.cost-img {
				width: 30rpx;
				height: 30rpx;
				margin-left: 10rpx;
			}

			.sale_value {
				font-size: 28rpx;
				color: #999;
			}
		}

	}

	.bottom_box {
		// background-color: #fff;
	}

	.sale_tips {
		width: 100%;
		background-color: $bg-box;
		padding: 28rpx 26rpx;
		border-radius: 20rpx;

		.sale_title {
			font-size: 32rpx;
			color: #141519;
			font-weight: bold;
		}

		.sale_content {
			font-size: 26rpx;
			color: #999;
			line-height: 46rpx;
			margin-top: 20rpx;
		}
	}

	.collection_info {
		width: 100%;
		display: flex;
		padding: 40rpx 30rpx;
		padding-bottom: 0;
		border-radius: 20rpx 20rpx 0 0;
		background-color: $bg-box;
		margin-top: 20rpx;

		.collect_image {
			width: 191rpx;
			height: 191rpx;
			border-radius: 10rpx;
			margin-right: 20rpx;
		}

		.collect_info {
			flex: 1;
			padding-top: 25rpx;

			.collect_title {
				font-size: 32rpx;
				color: #000;
				font-weight: bold;
				padding-bottom: 10rpx;
				border-bottom: 1rpx solid #D9D9D9;
			}

			.collect_item {
				padding-top: 10rpx;
				display: flex;
				align-items: center;

				.keys {
					width: 135rpx;
					font-size: 24rpx;
					color: #666;
				}

				.value {
					font-size: 24rpx;
					color: #141519;
					font-weight: bold;
				}
			}
		}
	}

	.sale_input {
		width: 100%;
		background: $bg-box;
		padding: 30rpx;
		border-radius: 20rpx;

		.sale_user {
			display: flex;
			align-items: center;
			height: 112rpx;
			font-size: 32rpx;
			color: #333;

			.sale_head {
				width: 46rpx;
				height: 46rpx;
				margin: 0 25rpx;
				margin-right: 10rpx;
				border-radius: 50%;
			}
		}

		.sale_title {
			font-size: 32rpx;
			color: #999;
		}

		.inputer {
			height: 118rpx;
			font-size: 68rpx;
			font-weight: bold;
			color: #141519;
		}

		.sale_holder {
			font-weight: normal;
			font-size: 68rpx;
			color: #999;
		}
	}

	page {
		background-color: #fafafc;
	}
</style>